package com.whnk.model.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.io.IOException;

import com.whnk.common.core.utils.bean.BeanUtils;
import com.whnk.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.whnk.model.domain.DefTWorkflow;
import com.whnk.model.service.IDefTWorkflowService;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 本地工作流Controller
 *
 * @author whnk
 * @date 2021-10-20
 */
@Api(tags = "本地工作流")
@RestController
@RequestMapping("/workflow")
public class DefTWorkflowController extends BaseController {
    @Autowired
    private IDefTWorkflowService defTWorkflowService;

    /**
     * 查询本地工作流列表
     */
    //@PreAuthorize(hasPermi = "model:workflow:list")
    @ApiOperation(value = "查询本地工作流列表")
    @GetMapping("/list")
    public TableDataInfo list(DefTWorkflow defTWorkflow) {
        startPage();
        List<DefTWorkflow> list = defTWorkflowService.selectDefTWorkflowList(defTWorkflow);
        for (DefTWorkflow tWorkflow : list) {
            if (tWorkflow.getWorkflowContent() != null) {
                tWorkflow.setXmlExist(true);
            } else {
                tWorkflow.setXmlExist(false);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出本地工作流列表
     */
    //@PreAuthorize(hasPermi = "model:workflow:export")
    //@Log(title = "导出本地工作流列表", businessType = BusinessType.EXPORT)
   /* @PostMapping("/export")
    public void export(HttpServletResponse response, DefTWorkflow defTWorkflow) throws IOException {
        List<DefTWorkflow> list = defTWorkflowService.selectDefTWorkflowList(defTWorkflow);
        ExcelUtil<DefTWorkflow> util = new ExcelUtil<DefTWorkflow>(DefTWorkflow.class);
        util.exportExcel(response, list, "本地工作流数据");
    }*/

    /**
     * 通过Id获取本地工作流详细信息
     */
    //@PreAuthorize(hasPermi = "model:workflow:query")
    @ApiOperation(value = "根据Id获取本地工作流信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(defTWorkflowService.selectDefTWorkflowById(id));
    }

    /**
     * 新增本地工作流
     */
    //@PreAuthorize(hasPermi = "model:workflow:add")
    //@Log(title = "新增本地工作流", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增本地工作流")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestPart(value = "file", required = false) MultipartFile file, DefTWorkflow defTWorkflow) {
        DefTWorkflow defTWorkflow1 = defTWorkflowService.selectDefTWorkflowByWorkflowCode(defTWorkflow.getWorkflowCode());
        if (null != defTWorkflow1) {
            return AjaxResult.error("工作流编码已存在");
        }
        DefTWorkflow defTWorkflow2 = defTWorkflowService.selectDefTWorkflowByWorkflowName(defTWorkflow.getWorkflowName());
        if (null != defTWorkflow2) {
            return AjaxResult.error("工作流名称已存在");
        }
        byte[] bytes = null;
        if (file != null && !file.isEmpty()) {
            try {
                bytes = file.getBytes();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (defTWorkflow.getOnlineXml() != null) {
            bytes = defTWorkflow.getOnlineXml().getBytes();
        }
        defTWorkflow.setWorkflowContent(bytes);
        return toAjax(defTWorkflowService.insertDefTWorkflow(defTWorkflow));
    }


    /**
     * 修改本地工作流
     */
    //@PreAuthorize(hasPermi = "model:workflow:edit")
    //@Log(title = "修改本地工作流", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改本地工作流")
    @PutMapping("edit")
    public AjaxResult edit(@RequestPart(value = "file", required = false) MultipartFile file, DefTWorkflow defTWorkflow) {
        String onlineXml = defTWorkflow.getOnlineXml();
        DefTWorkflow defTWorkflow1 = this.defTWorkflowService.selectDefTWorkflowByWorkflowCode(defTWorkflow.getWorkflowCode());
        if (defTWorkflow1 != null && !defTWorkflow1.getId().equals(defTWorkflow.getId())) {
            return AjaxResult.error("该工作流编码已存在");
        }
        DefTWorkflow defTWorkflow2 = this.defTWorkflowService.selectDefTWorkflowByWorkflowName(defTWorkflow.getWorkflowName());
        if (defTWorkflow2 != null && !defTWorkflow2.getId().equals(defTWorkflow.getId())) {
            return AjaxResult.error("该工作流名称已存在");
        }
        byte[] bytes = null;
        if (file != null && !file.isEmpty()) {
            try {
                bytes = file.getBytes();
                defTWorkflow.setWorkflowContent(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (!onlineXml.equals("null")) {
            bytes = defTWorkflow.getOnlineXml().getBytes();
            defTWorkflow.setWorkflowContent(bytes);
        } else {
            defTWorkflow.setWorkflowContent(defTWorkflowService.selectDefTWorkflowById(defTWorkflow.getId()).getWorkflowContent());
        }
        return toAjax(defTWorkflowService.updateDefTWorkflow(defTWorkflow));
    }

    /**
     * 通过Id删除本地工作流
     */
    //@PreAuthorize(hasPermi = "model:workflow:remove")
    //@Log(title = "删除本地工作流", businessType = BusinessType.DELETE)
    @ApiOperation(value = "通过Id集合删除本地工作流")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (null == ids || ids.length == 0) {
            return AjaxResult.error("删除本地工作流失败,参数有误");
        }
        return toAjax(defTWorkflowService.deleteDefTWorkflowByIds(ids));
    }


    /**
     * workflowCode查询流程图接口
     *
     * @param response
     * @param workflowCode
     * @throws IOException
     */
    //@PreAuthorize(hasPermi = "model:workflow:getXml")
    //@Log(title = "查询流程图接口", businessType = BusinessType.DELETE)
    @ApiOperation(value = "查询流程图接口")
    @GetMapping("/getDefinitionXML/{workflowCode}")
    public void getDefinitionXML(HttpServletResponse response, @PathVariable("workflowCode") String workflowCode) throws IOException {
        DefTWorkflow dwf = defTWorkflowService.selectDefTWorkflowByWorkflowCode(workflowCode);
        byte[] bytes = dwf.getWorkflowContent();
        if (bytes != null) {
            response.setContentType("text/xml");
            OutputStream outputStream = null;
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } else {
            response.sendError(40401, "there is noFile");
        }
    }


}
